/*!
 * \file SocketExpection.cpp
 * \brief Implements the SocketExpection class.
 */

#include "SocketException.h"
#ifdef WIN32
#include <windows.h>
#endif

using namespace Networking;

SocketException::SocketException () : _exception( GetError() )
{
}

SocketException::~SocketException () throw()
{
}

std::string SocketException::GetError ()
{
	/*
	* Errors are handled by calling the WSAGetLastError routine which 
	* will return the last error as one of the following. As we develop
	* this tutorial, we will go into much more detail on what they mean
	* and what caused them.
	*/
#ifdef WIN32
	int errorCode = WSAGetLastError();
	switch ( errorCode )
	{
	case WSANOTINITIALISED :
		return "Unable to initialise socket.\n";
		break;
	case WSAEAFNOSUPPORT :
		return "The specified address family is not supported.\n";
		break;
	case WSAEADDRNOTAVAIL :
		return "Specified address is not available from the local machine.\n";
		break;
	case WSAECONNREFUSED :
		return "The attempt to connect was forcefully rejected.\n"; 
		break;
	case WSAEDESTADDRREQ :
		return "address destination address is required.\n";
		break;
	case WSAEFAULT :
		return "The namelen argument is incorrect.\n";
		break;
	case WSAEINVAL :
		return "The socket is not already bound to an address.\n";
		break;
	case WSAEISCONN :
		return "The socket is already connected.\n";
		break;
	case WSAEADDRINUSE :
		return "The specified address is already in use.\n";
		break;
	case WSAEMFILE :
		return "No more file descriptors are available.\n";
		break;
	case WSAENOBUFS :
		return "No buffer space available. The socket cannot be created.\n";
		break;
	case WSAEPROTONOSUPPORT :
		return "The specified protocol is not supported.\n";
		break; 
	case WSAEPROTOTYPE :
		return "The specified protocol is the wrong type for this socket.\n";
		break;
	case WSAENETUNREACH :
		return "The network can't be reached from this host at this time.\n";
		break; 
	case WSAENOTSOCK :
		return "The descriptor is not a socket.\n";
		break;
	case WSAETIMEDOUT :
		return "Attempt timed out without establishing a connection.\n";
		break;
	case WSAESOCKTNOSUPPORT :
		return "Socket type is not supported in this address family.\n";
		break;
	case WSAENETDOWN :
		return "Network subsystem failure.\n";
		break;
	case WSAHOST_NOT_FOUND :
		return "Authoritative Answer Host not found.\n";
		break;
	case WSATRY_AGAIN :
		return "Non-Authoritative Host not found or SERVERFAIL.\n";
		break;
	case WSANO_RECOVERY :
		return "Non recoverable errors, FORMERR, REFUSED, NOTIMP.\n";
		break;
	case WSANO_DATA :
		return "Valid name, no data record of requested type.\n";
		break;
	case WSAEINPROGRESS :
		return "address blocking Windows Sockets operation is in progress.\n";
		break;
	case WSAEINTR :
		return "The (blocking) call was canceled via WSACancelBlockingCall().\n";
		break;
	case WSAEWOULDBLOCK :
		return "The socket is marked as nonblocking and SO_LINGER is set to a nonzero time-out value.";
		break;
	default :
		return "Unknown error.\n";
		break;
	}
#else
	return "Socket Error";
#endif
}
